package com.lz.yz.member.service.impl;

import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.core.AbstractService;
import com.lz.yz.empowerment.model.LableModel;
import com.lz.yz.member.dao.LableMapper;
import com.lz.yz.member.dao.UserLableMapper;
import com.lz.yz.member.entity.Lable;
import com.lz.yz.member.entity.UserLable;
import com.lz.yz.member.model.MemberLableModel;
import com.lz.yz.member.service.AccountService;
import com.lz.yz.member.service.UserLableService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;



@Service
@Transactional
public class UserLableServiceImpl extends AbstractService<UserLable> implements UserLableService {
    @Resource
    private UserLableMapper userLableMapper;

    @Autowired
    AccountService accountService;

    @Autowired
    private LableMapper lableMapper;


    /**
     *  用户添加自定义标签
     * @param
     * @return
     */
    @Override
    public ReturnValue addMemberLable(MemberLableModel memberLableModel) {
        List<Lable>lables= userLableMapper.queryMemberLable(memberLableModel.getAccountId());
        if(null!=lables){
            for(Lable ls:lables){
                if(ls.getLableName().equalsIgnoreCase(memberLableModel.getLableName())){
                    return   ReturnValue.generateFalseReturnValue("该标签已存在，请勿重复添加");
                }
            }
            Lable lable=new Lable();
            lable.setLableName(memberLableModel.getLableName());
            lableMapper.insertSelective(lable);
            UserLable userLable=new UserLable();
            userLable.setAccountId(memberLableModel.getAccountId());
            userLable.setLableId(lable.getId());
            userLableMapper.insertSelective(userLable);
            return   ReturnValue.generateTrueReturnValue(lable);
           // return ReturnValue.generateTrueReturnValue("标签添加成功");
        }
        return ReturnValue.generateTrueReturnValue("该用户没有默认标签");
    }



    /**
     *  查询当前用户标签
     * @param
     * @return
     */
    @Override
    public ReturnValue queryMemberLable(Integer accountId) {
        List<LableModel> lableModels = new ArrayList<>();
        List<Lable> lables = userLableMapper.queryMemberLable(accountId);
        if (null != lables){
            lables.forEach(lable -> {
                LableModel lableModel = new LableModel();
                BeanUtils.copyProperties(lable, lableModel);
                lableModels.add(lableModel);
            });
            return ReturnValue.generateTrueReturnValue(lableModels);
        }
        return ReturnValue.generateTrueMsgReturnValue("该用户没有标签");
    }

    /**
     *  删除用户标签
     * @param
     * @return
     */
    @Override
    public ReturnValue deleteMemberLable(MemberLableModel memberLableModel){
        UserLable userLable=new UserLable();
        userLable.setType(memberLableModel.getType());
        userLable.setLableId(memberLableModel.getLableId());
        userLable.setAccountId(memberLableModel.getAccountId());
        userLableMapper.delete(userLable);
       if(memberLableModel.getType()==0){
           lableMapper.deleteByIds(userLable.getLableId().toString());
       }
        return ReturnValue.generateTrueMsgReturnValue("删除标签成功");
    }
}
